package com.star.spring.easyexcel.controller;

import java.util.List;

import lombok.extern.slf4j.Slf4j;

import com.star.spring.easyexcel.model.ImportDTO;
import com.star.spring.easyexcel.utils.ExcelUtil;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@RestController
@RequestMapping("/import")
public class ImportController {

    @Value("${image.save.path:/tmp/excel-images}")
    private String imageSavePath;

    @PostMapping("/simple")
    public String simple(@RequestPart("file") MultipartFile file){

        List<ImportDTO> importDTOS = ExcelUtil.readExcel(file, ImportDTO.class);
        log.info(importDTOS.toString());
        return "success";

    }

    @PostMapping("/with-images")
    public String withImages(@RequestPart("file") MultipartFile file){

        List<ImportDTO> importDTOS = ExcelUtil.readExcelWithImages(file, ImportDTO.class, imageSavePath);
        log.info(importDTOS.toString());
        return "success with images";

    }
}
